package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;


import ModelLayer.Loan;

public class DBLoan {
	private static Connection con;
	Statement stmt;
	public DBLoan() 
	{
		con = DbConnection.getInstance().getDBcon();
	}
	public void createLoan(int amount,String l_status,String l_date,int l_duration,int cID)
	{	String query="";
		try{
			int lID=GetMax.getMaxId("Select max(lID) from Loan")+1;//Gets the maximum lID from the DB and adds+1 for the next ID
			
			 query ="INSERT INTO Loan( lID, amount, l_status, l_date, l_duration, cID)  VALUES('"+lID+"','"+amount+"','"+l_status+"','"+l_date+"','"+l_duration+"','"+cID+"')";
			
			 Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  stmt.executeUpdate(query);
	          stmt.close();
		}
		catch(Exception e)
		{	//System.out.println("SuID,clID or gID is most probably invalid!");
			e.printStackTrace();
			}
	}
	public void deleteLoan(int lID)
	{	//lID=1; 
		try{
			String query="DELETE FROM Loan WHERE lID = '"+lID+"'";
			Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  stmt.executeUpdate(query);
	          stmt.close();
		}
		catch(Exception e)
		{e.printStackTrace();
		}
	}
	public void updateLoan(Loan g)
	{ Loan suObj = g;
	String query="UPDATE Loan SET "+
		 	  "amount ='"+ suObj.getAmount()+"', "+
		 	 "l_status ='"+ suObj.getL_status() + "'" +
		 	"l_date ='"+ suObj.getL_date() + "'" +
		 	"l_duration ='"+ suObj.getcID() + "'" +
		 	"cID ='"+ suObj.getcID() + "'" +
		 	      " WHERE lID = '"+ suObj.getlID() + "'";
              System.out.println("Update query:" + query);
              try{
            	  Statement stmt = con.createStatement();
            	  stmt.setQueryTimeout(5);
            	  stmt.executeUpdate(query);
            	  stmt.close();
            	  
              }
              catch(Exception e)
              {e.printStackTrace();}
		
	}
	
	private String buildQuery()
	{
	    String query="SELECT lID, amount, l_status, l_date, l_duration, cID  FROM Loan";
		/*
		if (wClause.length()>0)
			query=query+" WHERE ";
			*/
		return query;
	}
	public ArrayList<Loan> miscWhere()
	{
            ResultSet results;
	    ArrayList<Loan> list = new ArrayList<Loan>();	
		
	    String query =  buildQuery();
  
            try{ // read the employee from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 Loan empObj = new Loan();
		 empObj = buildLoan(results);	
                 list.add(empObj);	
		}//end while
                 stmt.close();     
                 
			
		}//slut try	
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	public Loan findLoan(int lID)
	{
		ResultSet results;
		Loan empObj = new Loan();
                
	        String query =  buildQuery() + " WHERE lID ="+"'"+lID+"'";
                System.out.println(query);
		try{ // 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            empObj = buildLoan(results);
                            //assocaition is to be build
                            stmt.close();
                            
                            
			}
                        else{ //no Loan was found
                            empObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return empObj;
	}
	private Loan buildLoan(ResultSet results)
    {   Loan suObj = new Loan();
        try{ // the columns from the table glayee  are used
        	//lID, date, quantity, delivery_status, delivery_date, cID, pID, iID
              suObj.setlID(results.getInt("lID"));
              suObj.setAmount(results.getInt("date"));
              suObj.setL_status(results.getString("l_status"));
              suObj.setL_date(results.getString("l_date"));
              suObj.setL_duration(results.getInt("l_duration"));
              suObj.setcID(results.getInt("cID"));
              
              
              
        }
       catch(Exception e)
       {
           System.out.println("Error in building the Loan object");
       }
       return suObj;
}
	
}